<?php

namespace app\controller\api\property;

use app\common\repositories\housing\PropertyFacilityRepository;
use app\controller\api\Base;
use app\validate\housing\PropertyFacilityValidate;
use think\App;

class PropertyFacility extends Base
{
    protected $repository;

    public function __construct(App $app, PropertyFacilityRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    /**
     * 获取物业设施列表
     * @return mixed
     *
     * @author Amos
     * 2024-06-12 23:18
     */
    public function list()
    {
        $where = $this->request->param([
            'keywords' => '',
        ]);
        [$page, $limit] = $this->getPage();
        $res = $this->repository->getApiList($where, $page, $limit, $this->request->propertyId);
        return $this->success($res);
    }

    public function add()
    {
        $param = $this->request->param([
            'facility_name' => '',
            'picture' => '',
            'is_show' => '',
            'remark' => '',
        ]);
        $param['is_show'] = 1;
        validate(PropertyFacilityValidate::class)->scene('add')->check($param);
        $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId());
        return app('api_return')->success('添加成功');
    }

    public function edit(int $id)
    {
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $param = $this->request->param([
            'facility_name' => '',
            'picture' => '',
            'is_show' => '',
            'remark' => '',
        ]);
        validate(PropertyFacilityValidate::class)->scene('edit')->check($param);
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        $this->repository->editInfo($info, $param, $this->request->userId());
        return app('api_return')->success('更新成功');
    }


    public function delete(int $id)
    {
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        $res = $this->repository->batchDelete([$id], $this->request->userId());
        if ($res) {
            return $this->successText('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }
}